package com.yang.controller;

import com.yang.service.CheckOutService;
import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
public class CheckOutController {

    private static Logger logger = Logger.getLogger(CheckOutController.class);

    @Resource
    CheckOutService checkOutService;

    /**
     *续租检测
     * @param reletRoomNum
     * @param preDay
     * @return
     */
    @RequestMapping(produces = "application/json;charset=UTF-8",value = "admin/detection",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> detection(String reletRoomNum,String preDay){
        Map<String,Object> map = new HashMap<String, Object>();

        if(reletRoomNum == null||reletRoomNum.equals("") || preDay==null ||preDay.equals("")){
            map.put("status","error");
            map.put("info","输入不能为空！");
            return map;
        }
        int day = Integer.parseInt(preDay);
        map = checkOutService.dectection(reletRoomNum,day);
        return map;
    }

    /**
     * 续租
     * @param reletRoomNum
     * @param preDay
     * @param prepay
     * @return
     */
    @RequestMapping(produces = "application/json;charset=UTF-8",value = "admin/relet",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> relet(String reletRoomNum,String preDay,String prepay){
        Map<String,Object> map = new HashMap<String, Object>();
        if(reletRoomNum == null||reletRoomNum.equals("") || preDay==null ||preDay.equals("")||prepay==null||prepay.equals("")){
            map.put("status","error");
            map.put("info","输入不能为空！");
            return map;
        }
        int day = Integer.parseInt(preDay);
        float money = Float.parseFloat(prepay);
        map = checkOutService.relet(reletRoomNum,day,money);
        return map;
    }

    /**
     * 退订或结账
     * @param overRoomNum
     * @return
     */
    @RequestMapping(produces = "application/json;charset=UTF-8",value = {"admin/settleAccounts","customer/settleAccounts"},method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> settleAccounts(String overRoomNum){
        Map<String ,Object> map = new HashMap<String, Object>();
        if(overRoomNum == null || overRoomNum.equals("")){
            map.put("status","error");
            map.put("info","不让输入空值！");
            logger.error("输入空值！");
            return map;
        }
        map = checkOutService.settleAccounts(overRoomNum);
        return map;
    }

    /**
     * 查询未结账
     * @param roomNum
     * @param name
     * @return
     */
    @RequestMapping(produces = "application/json;charset=UTF-8",value = {"admin/select_unchecked"},method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> selectUnchecked(String roomNum,String name){
        Map<String,Object> map = new HashMap<String, Object>();
        map = checkOutService.selectUnchecked(roomNum,name);
        return map;
    }

    /**
     * 结算未结账
     * @param orderId
     * @return
     */
    @RequestMapping(produces = "application/json;charset=UTF-8",value = {"admin/finish_unchecked"},method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> finishUnchaecked(String orderId){
        Map<String,Object> map = new HashMap<String, Object>();
        if(StringUtils.isBlank(orderId)){
            map.put("status","error");
            map.put("info","请输入结算编号");
            logger.error("结账编号未空");
            return map;
        }
        long longOrderId = Long.parseLong(orderId);
        map = checkOutService.finishUnchecked(longOrderId);
        return map;
    }

}
